// pages/home/home.js
import urlconf from '../../utils/urlconf.js'
import httpRequest from '../../utils/request.js'
const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        examList: [],
        categoryItem: [],
        currentTab: 0, //预设导航筛选
        pop_content: '',
        noticeTag: false,
        notice_status: false,
        announContent: '',
        showConfirm: false, //弹框显示确认按钮
        duration: 3000,
        courseJump: false
    },
    showNotice() {
        let noticeTag = !this.data.noticeTag
        this.setData({
            noticeTag
        })
    },
    goXtj() {
        wx.navigateToMiniProgram({
            appId: 'wx9a7762ec960c6da3',
            path: 'pages/index/index',
            extraData: {},
            envVersion: 'release',
            success(res) {
                // 打开成功 
            }
        })
    },
    swichNav: function (e) {
        let { current, name } = e.target.dataset;
        wx.setStorageSync('categoryCache', name)
        if (this.data.currentTab != current) {
            this.setData({
                currentTab: current
            }, () => {
                this.examInit(name)
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        if (options.categoryCache) {
            wx.setStorageSync('categoryCache', options.categoryCache)
        }
        let appId = app.globalData.appId
        wx.setNavigationBarTitle({
            title: appId == 'wxe64965622c7a7fdb' ? '省考排名' : '估分助手'
        })
        let currentTab;
        httpRequest(urlconf.announcement, {}, 'GET').then(res => {
            if (res.code == 1) {
                this.setData({
                    announContent: res.content,
                    notice_status: true
                })
                if (wx.getStorageSync("announcement") != '1') { //首次提醒
                    wx.setStorageSync('announcement', '1');
                    this.setData({
                        noticeTag: true,
                    })
                }
            }
        })
        httpRequest(urlconf.getAreaList, {}, 'GET').then(res => {
            if (res.code == 1) {
                res.data.map((item, i) => {
                    if (item == wx.getStorageSync('categoryCache')) {
                        currentTab = i
                    }
                })
                this.setData({
                    categoryItem: res.data,
                    currentTab
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let categoryCache = wx.getStorageSync('categoryCache') || '最新'
        this.examInit(categoryCache)
    },
    examInit(name) {
        httpRequest(urlconf.examList, { area: name }, 'GET').then(res => {
            if (res.code == 1) {
                this.setData({
                    examList: res.data
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let shareImgUrl = app.globalData.shareImg
        return {
            imageUrl: shareImgUrl,
            path: '/pages/index/index'
        };
    },
    toQuestion(e) {
        console.log(e);
        let { examid, status, nums, name, empty, openstatus, gfMessage } = e.currentTarget.dataset
        if (openstatus == 2) {
            this.setData({
                loading: true,
                pop_content: gfMessage ? gfMessage : '暂未开始',
                showConfirm: false,
                duration: 2000
            })
            return;
        }
        if (empty) {
            wx.navigateTo({
                url: `../examNull/examNull?examId=${examid}&&nums=${nums}&&name=${name}`
            })
            return;
        }
        if (status) {
            wx.navigateTo({
                url: `../examResult/examResult?examId=${examid}&&nums=${nums}&&lastPage=home&&name=${name}`
            })
        } else {
            wx.navigateTo({
                url: `../question/question?examId=${examid}&&nums=${nums}&&name=${name}`
            })
        }
    },
    confirmEvent(e) {
        console.log(e)
        if (this.data.courseJump) { //跳转课程
            wx.navigateToMiniProgram({
                appId: 'wxa02587af776cd926',
                path: this.data.courseurl,
                extraData: {},
                envVersion: 'release',
                success(res) {
                    // 打开成功 
                }
            })
        }
        this.setData({
            loading: false,
            showConfirm: false,
            duration: 2000,
            courseJump: false
        })
    }
})